/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.market.web.EJB;

import com.market.web.model.Customer;
import com.market.web.model.Inventory;
import com.market.web.model.Item;
import com.market.web.model.Order1;
import com.market.web.model.Orderdetails;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author ps
 */

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class OrderTransaction {
    @PersistenceContext(unitName = "onlineMarketPU")
    private EntityManager em;
    @Resource
    private SessionContext context;
    @EJB
    private InventoryFacade inventoryFacade;
//    @EJB
//    private Order1Facade orderFacade;
//    @EJB
//    private OrderdetailsFacade orderDetailFacade;
    DecimalFormat f = new DecimalFormat("##.00");
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public int makeOrder(Customer c,AllItems items)
    {
        try
        {
            Order1 order = addOrder(c,items);
            addOrderDetail(order, items);
            return order.getOrderId();
        }
        catch(Exception e)
        {
            context.setRollbackOnly();
            return 0;
        }
    }
    
    private Order1 addOrder(Customer cust,AllItems items)
    {
        
        Order1 order = new Order1();
        order.setOrderId(1);
        order.setCustId(cust);
        order.setDateTime(new Date());
        BigDecimal total = new BigDecimal(f.format(items.getTotal()));
        order.setTotalCost(total);
        
        em.persist(order);
        return order;
        
    }
    
    private void addOrderDetail(Order1 o,AllItems allItems)
    {
        em.flush();
        int count =111;
        List<Item> items = allItems.getItems();
        for(Item i:items)
        {
            int productId = i.getId();
            int orderId = o.getOrderId();
            int productQty = i.getQty();
            String productName = i.getName();
            int remainQty;
            
            Inventory inv = inventoryFacade.find(productId);
            remainQty = inv.getQty() - productQty;
            inv.setQty(remainQty);
            em.merge(inv);
            
            Orderdetails orderDet = new Orderdetails();
            orderDet.setOrderDetailsId(count++);
            orderDet.setOrderId(o);
            orderDet.setPid(inv);
            orderDet.setOrderQty(productQty);
            
            em.flush();
            orderDet.setProductName(productName);
            em.persist(orderDet);
            
        }
    }

}
